import { BASE_URL } from '../../common/config';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bookid:'',
    bookDetail:{},
    bookAppraise:[
      {'text':"好看",'progress':"0"},
      {'text':"一般",'progress':"0"},
      {'text':"不行",'progress':"0"},
    ],
    show: false,
    popupText:'',//简介弹窗内容
    bookRecommended:[],
    reviewRating:"",//评价等级
    subscribe:true,//订阅状态
    fileUrl:""//文件下载地址
  },
  showPopup() {
    this.setData({
      show: true,
      popupText:this.data.bookDetail.book_introduce
    });
  },
  onClose() {
    this.setData({ show: false });
  },
  jumpComments(){
    wx.navigateTo({
      url: '/pages/bookReviews/bookReviews?bookId='+this.data.bookDetail.book_id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let bookid=options.bookid
    // console.log(bookid)
    this.setData({
      bookid:bookid
    })
    this.getBookDetail(bookid)//获取图书详情
    this.getBookReviewRating()//获取当前用户对此书的评价等级
    this.getRandomBook()//获取书籍推荐
    this.getProgress()//获取评论进度
    this.getSubscribe()//获取当前用户对此书的订阅状态
    this.getBookFileUrl(bookid)//获取文件下载地址
  },
  getBookDetail(bookid){
    wx.request({
      url: `${BASE_URL}/BookDetail`,
      method:'POST',
      data:{
        bookId:bookid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success:(res)=>{
        // console.log("请求成功：",res.data.data[0])
        this.setData({
          bookDetail:res.data.data[0]
        })
      },
      fail:(err)=>{}
    })
  },
  getRandomBook(){
    wx.request({
      url: `${BASE_URL}/RandomBook`,
      method:'POST',
      data:{
        bookId:this.data.bookid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success:(res)=>{
        this.setData({
          bookRecommended:res.data.data
        })
      },
      fail:(err)=>{}
    })
  },
  getBookReviewRating(){
    let openid=wx.getStorageSync('openid')
    wx.request({
      url: `${BASE_URL}/UserComments`,
      method:'POST',
      data:{
        bookId:this.data.bookid,
        openId:openid
      },
      success:(res)=>{
        // console.log(res.data)
        this.setData({
          reviewRating:res.data
        })
      },
      fail:(err)=>{}
    })
  },
  getProgress(){
    wx.request({
      url: `${BASE_URL}/CommentsProgress`,
      method:'GET',
      data:{
        bookId:this.data.bookid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success:(res)=>{
        // console.log(res.data.data)
        this.setData({
          totalComments:res.data.data.totalComments,
          bookAppraise: [
            {text: "好看",progress:res.data.data.goodRate.toString()},
            {text: "一般",progress:res.data.data.ordinaryRate.toString()},
            {text: "不行",progress:res.data.data.badRate.toString()}
          ],
          svgColor : res.data.data.goodRate >= 80 ? '#4CAF50' : 
           res.data.data.goodRate >= 60 ? '#8BC34A' : 
           res.data.data.goodRate >= 40 ? '#FFEB3B' : 
           res.data.data.goodRate >= 20 ? '#FF9800' : '#F44336'
        });
      },
      fail:(err)=>{}
    })
  },
  jumpBookDetail(e){
    let bookid=e.currentTarget.dataset.bookid
    wx.navigateTo({
      url: '/pages/bookDetail/bookDetail?bookid='+bookid,
    })
  },
  jumpInputReviews(e){
    let reviewRating=e.currentTarget.dataset.review
    let bookId=this.data.bookid
    let bookname=this.data.bookDetail.book_name
    wx.navigateTo({
      url: '/pages/inputReviews/inputReviews?reviewRating='+reviewRating+'&bookId='+bookId+'&bookname='+bookname,
    })
  },
  jumpReadBook(){
    let bookid=this.data.bookid
    let bookname=this.data.bookDetail.book_name
    wx.navigateTo({
      url: '/pages/readBook/readBook?bookid='+bookid+'&bookname='+bookname,
    })
  },
  jumpDownloadFile(){
    // console.log("1")
    // this.setData({
    //   webSrc:"http://10.20.4.192:8080/"
    // })
    let fileUrl=this.data.fileUrl
    // console.log(fileUrl)
    wx.setClipboardData({
      data: fileUrl,
      success (res) {
        wx.hideLoading();
        wx.showToast({
          title: `已复制下载链接，请于浏览器中打开`,
          icon:"none",
          duration:3500
        })
      }
    })
    // wx.navigateTo({
    //   url: '/pages/downloadFile/downloadFile?fileUrl='+fileUrl,
    // })
  },
  changeSubscribe(){
    if(this.data.subscribe){
      this.setData({
        subscribe:!this.data.subscribe        
      })
      this.saveSubscribe(true)
      wx.showToast({
        title: '成功加入订阅',
        icon:'none',
        duration: 1500
      })
    }else{
      wx.showModal({
        title: '取消订阅',
        content: '是否确认取消订阅',
        success:(res)=>{
          if (res.confirm) {
              this.setData({
                subscribe:!this.data.subscribe
              })
              this.saveSubscribe(false)
              wx.showToast({
                title: '成功取消订阅',
                icon:'none',
                duration: 1500
              })
          }
        }
      });
    }
  },
  saveSubscribe(subscribe){
    let openid=wx.getStorageSync('openid')
    wx.request({
      url: `${BASE_URL}/saveSubscribe`,
      method:'POST',
      data:{
        bookId:this.data.bookid,
        openId:openid,
        state:subscribe
      },
      success:(res)=>{
        // console.log(res)
      },
      fail:(err)=>{}
    })
  },
  getSubscribe(){
    let openid=wx.getStorageSync('openid')
    wx.request({
      url: `${BASE_URL}/getSubscribe`,
      method:'POST',
      data:{
        bookId:this.data.bookid,
        openId:openid
      },
      success:(res)=>{
        // console.log(res)
        this.setData({
          subscribe:!res.data
        })
      },
      fail:(err)=>{}
    })
  },
  getBookFileUrl(bookid){
    wx.request({
      url: `${BASE_URL}/BookFileUrl`,
      method:'GET',
      data:{
        bookId:bookid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success:(res)=>{
        // console.log(res)
        this.setData({
          fileUrl:res.data.data
        })
      },
      fail:(err)=>{}
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getBookReviewRating()
    this.getProgress()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})